package entities.extra;

import entities.Trip;
import entities.User;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "user_trip_participation")
@NamedQueries({
    @NamedQuery(name = "UserTripParticipation.findAll", query = "SELECT u FROM UserTripParticipation u"),
    @NamedQuery(name = "UserTripParticipation.findByIdUser", query = "SELECT u FROM UserTripParticipation u WHERE u.userTripParticipationPK.idUser = :idUser"),
    @NamedQuery(name = "UserTripParticipation.findByIdTrip", query = "SELECT u FROM UserTripParticipation u WHERE u.userTripParticipationPK.idTrip = :idTrip"),
    @NamedQuery(name = "UserTripParticipation.findAppointedTripFor", query = "SELECT u FROM Vehicle v, UserTripParticipation u, Trip p WHERE v.idUser.idUser = u.userTripParticipationPK.idUser and v.idUser.idUser != p.idVehicle.idUser.idUser and u.userTripParticipationPK.idUser = ?1 and p.idTrip = u.userTripParticipationPK.idTrip and p.ended = false"),
    @NamedQuery(name = "UserTripParticipation.findTripHistoricFor", query = "SELECT u FROM UserTripParticipation u, Trip p WHERE u.userTripParticipationPK.idUser = ?1 and p.idTrip = u.userTripParticipationPK.idTrip and p.ended = true"),
    @NamedQuery(name = "UserTripParticipation.findByAccepted", query = "SELECT u FROM UserTripParticipation u WHERE u.accepted = :accepted")})
public class UserTripParticipation implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @EmbeddedId
    protected UserTripParticipationPK userTripParticipationPK;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "accepted")
    private boolean accepted;
    
    @JoinColumn(name = "idUser", referencedColumnName = "idUser", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user;
    
    @JoinColumn(name = "idTrip", referencedColumnName = "idTrip", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Trip trip;

    public UserTripParticipation() {
    }

    public UserTripParticipation(User user, Trip trip, boolean accepted) {
        this.userTripParticipationPK = new UserTripParticipationPK(user.getId(), trip.getId());
        this.accepted = accepted;
        this.user = user;
        this.trip = trip;
    }

    public UserTripParticipationPK getParticipationKey() {
        return userTripParticipationPK;
    }

    public boolean getAccepted() {
        return accepted;
    }

    public void setAccepted(boolean accepted) {
        this.accepted = accepted;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Trip getTrip() {
        return trip;
    }

    public void setTrip(Trip trip) {
        this.trip = trip;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userTripParticipationPK != null ? userTripParticipationPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserTripParticipation)) {
            return false;
        }
        UserTripParticipation other = (UserTripParticipation) object;
        return !((this.userTripParticipationPK == null && other.userTripParticipationPK != null) || (this.userTripParticipationPK != null && !this.userTripParticipationPK.equals(other.userTripParticipationPK)));
    }

    @Override
    public String toString() {
        return "entities.extra.UserTripParticipation[ userTripParticipationPK=" + userTripParticipationPK + " ]";
    }

}
